#include "stdafx.h"
#include "MsgHandlerDispatchByCate.h"



MsgHandlerDispatchByCate::MsgHandlerDispatchByCate()
{

}

MsgHandlerDispatchByCate::~MsgHandlerDispatchByCate()
{
	MsgMap::iterator it = m_msgMap.begin();
	for(; it != m_msgMap.end(); ++it)
	{
		if(it->second)
		{
			delete it->second;
			it->second = NULL;
		}
	}
	m_msgMap.clear();
}

bool MsgHandlerDispatchByCate::initialize(IMsgHandlerInitData* data)
{
	return true;
}

bool MsgHandlerDispatchByCate::handleMsg(IMsg* pMsg)
{
	if(!pMsg)
		return false;

	MsgMap::iterator iter = m_msgMap.find(pMsg->getMsgCate());
	if (iter != m_msgMap.end())
	{
		return iter->second->handleMsg(pMsg);
	}
	else
	{
		LOG_DEBUG_V("Error msg category (%d)!!!", pMsg->getMsgCate());
		return false;
	}
}


void MsgHandlerDispatchByCate::addSubHandler(int nMsgCate, int nMsgID, IMsgHandler* pHandler)
{
	if(!pHandler)
		return;

	MsgMap::iterator it = m_msgMap.find(nMsgCate);
	if(it == m_msgMap.end())
	{
		m_msgMap.insert(std::make_pair(nMsgCate, pHandler));
	}
	else
	{
		LOG_ERROR_V("Msg category (%d) already exist!!! ", nMsgCate);
		return;
	}
	
}

void MsgHandlerDispatchByCate::rmvSubHandler(int nMsgCate, int nMsgID)
{
}

